package com.aaa.member.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.Result;
import com.aaa.member.dao.MemberDao;
import com.aaa.member.entity.Member;
import com.aaa.member.service.MemberService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员表(Member)表服务实现类
 *
 * @author makejava
 * @since 2020-11-17 20:42:37
 */
@Service("memberService")
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberDao memberDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Member queryById(Long id) {
        return this.memberDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Member> queryAllByLimit(int offset, int limit) {
        return this.memberDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @Override
    public Member insert(Member member) {
        this.memberDao.insert(member);
        return member;
    }

    /**
     * 修改数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @Override
    public Member update(Member member) {
        this.memberDao.update(member);
        return this.queryById(member.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.memberDao.deleteById(id) > 0;
    }

    @Override
    public Member queryMemberByParam(String phone) {
        Member member = new Member();
        member.setPhone(phone);
        List<Member> members = memberDao.queryAll(member);
        if(members != null && members.size() > 0){
            return members.get(0);
        }
        return null;
    }

    @Override
    public Result memberLogin(String phone, String passWord) {
        // 收集用户信息
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(phone, passWord);
        //获取主体对象
        Subject currentUser = SecurityUtils.getSubject();
        try {
            //登录（执行认证过程）
            currentUser.login(usernamePasswordToken);
            //成功后操作
            Member member =  (Member) currentUser.getPrincipal();
            //生成token
            //String token = UUID.randomUUID().toString();
            //String token = phone+System.currentTimeMillis();
            //放入redis
            //redisTemplate.opsForValue().set(token,user,30, TimeUnit.HOURS);
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(),"登陆成功！");
        }  catch (AuthenticationException e) {
            e.printStackTrace();
        }
        return new Result(ExceptionConstant.ERROR_USERNAME_PASSWORD.getErrorCode(),
                ExceptionConstant.ERROR_USERNAME_PASSWORD.getErrorMessage(),null);
    }
}